<template>
  <wd-input
    v-model="formData.recordTitle"
    type="text"
    label="标题"
    placeholder="请输入标题"
    required
  />
  <wd-textarea
    v-model="formData.recordContent"
    placeholder="在此输入日常..."
    :maxlength="1000"
    show-word-limit
  />
  <view style="text-align: right; padding: 10px">
    <wd-button type="success" @click="handleSubmit">保存</wd-button>
  </view>
  <wd-card>
    <view style="padding: 10px 10px 0px 0px">
      <wd-upload
        :file-list="fileList"
        :limit="9"
        :upload-method="customUpload"
        @remove="handleRemove"
      >
      </wd-upload>
    </view>
  </wd-card>
</template>
<script setup lang="js">
import { ref } from "vue";
import FileAPI from "@/api/file";
import { getToken } from "@/utils/cache";
import DailyRecordAPI from "@/api/daily/daily-record";
const formData = ref({});
const fileList = ref([]);
onLoad(({ id }) => {
  if (id) {
    DailyRecordAPI.getFormData(id).then((res) => {
      formData.value=res
      let obj={
        "uid": null,
        "name": "",
        "thumb": "",
        "status": "",
        "size": null,
        "url": "",
        "percent": 100,
        "response": ""
      }
      let dataList=[]
      JSON.parse(res.images).forEach(item=>{
        obj.url=item.url
        obj.uid=item.uid
        obj.thumb=item.url
        obj.status='success'
        obj.size=20000
        dataList.push(obj)
      })
      fileList.value=dataList
    });
  }
});
const handleSubmit = () => {
  formData.value.images = JSON.stringify(fileList.value);
  if (formData.value.id){
    DailyRecordAPI.update(formData.value.id,formData.value).then((res) => {
      uni.showToast({
        title: "操作成功",
        icon: "none",
      });
      setTimeout(() => {
        uni.switchTab({
          url: "/pages/daily/index",
        });
      }, 700);
    });
  }else {
    DailyRecordAPI.add(formData.value).then((res) => {
      uni.showToast({
        title: "操作成功",
        icon: "none",
      });
      setTimeout(() => {
        uni.switchTab({
          url: "/pages/daily/index",
        });
      }, 700);
    });
  }

};
const handleRemove = ({ file }) => {
  let data = [];
  fileList.value.forEach((item) => {
    if (item.uid !== file.uid) {
      console.log(item.uid, file.uid, 123123123123);
      data.push(item);
    }
  });
  fileList.value = data;
};
const customUpload = (file, formData, options) => {
  console.log(file);
  const uploadTask = uni.uploadFile({
    url: FileAPI.uploadUrl,
    filePath: file.url,
    name: "file",
    header: {
      Authorization: getToken() ? `Bearer ${getToken()}` : "",
    },
    formData: {},
    success: (res) => {
      if (res.statusCode === options.statusCode) {
        let data = JSON.parse(res.data);
        fileList.value.push({ uid: file.uid, url: data.data.url });
        // 设置上传成功
        options.onSuccess(res, file, formData);
      } else {
        // 设置上传失败
        options.onError({ ...res, errMsg: res.errMsg || "" }, file, formData);
      }
    },
    fail: (error) => {
      console.log("fail error", error);
      uni.showToast({
        title: "文件上传请求失败",
        icon: "none",
        duration: 2000,
      });
    },
  });
  uploadTask.onProgressUpdate((res) => {
    options.onProgress(res, file);
  });
};
</script>
<style scoped lang="scss"></style>
